﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using XCI.Config;

namespace XCI.Core
{
    /// <summary>
    /// 行政区域管理
    /// </summary>
    public static class AreaManager
    {
        private readonly static JsonDataFile<List<Area>> Province = new JsonDataFile<List<Area>>("Province");
        private readonly static JsonDataFile<List<Area>> City = new JsonDataFile<List<Area>>("City");
        private readonly static JsonDataFile<List<Area>> District = new JsonDataFile<List<Area>>("District");

        ///// <summary>
        ///// 设置编辑器的DisplayMember和ValueMember
        ///// </summary>
        ///// <param name="editors">编辑器数组</param>
        //public static void SetEditorMember(params IComboEditor[] editors)
        //{
        //    if (editors == null || editors.Length == 0)
        //    {
        //        return;
        //    }
        //    foreach (var editor in editors)
        //    {
        //        editor.DisplayMember = "Name";
        //        editor.ValueMember = "Code";
        //    }
        //}

        /// <summary>
        /// 获取省份信息
        /// </summary>
        public static ReadOnlyCollection<Area> GetProvince()
        {
            return Province.Data.OrderBy(p => p.Code).ToList().AsReadOnly();
        }

        /// <summary>
        /// 获取城市信息
        /// </summary>
        /// <param name="provinceCode">省份编码</param>
        public static ReadOnlyCollection<Area> GetCity(string provinceCode)
        {
            if (string.IsNullOrEmpty(provinceCode)) return null;
            var pro = provinceCode.Substring(0, 2);
            return City.Data.Where(p => p.Code.Substring(0, 2).Equals(pro)).OrderBy(p => p.Code).ToList().AsReadOnly();
        }

        /// <summary>
        /// 获取区县信息
        /// </summary>
        /// <param name="cityCode">城市编码</param>
        public static ReadOnlyCollection<Area> GetDistrict(string cityCode)
        {
            if (string.IsNullOrEmpty(cityCode)) return null;
            var ci = cityCode.Substring(0, 4);
            return District.Data.Where(p => p.Code.Substring(0, 4).Equals(ci)).OrderBy(p => p.Code).ToList().AsReadOnly();
        }
    }
}